package com.itheima.loop;

public class ForDemo3 {
    public static void main(String[] args) {
        //目标:求奇数和
        int result1 = test1(100);
        int result2 = test2(100);
    }

    public static int test1(int n) {
        //1.定义变量累加奇数
        int sum = 0;
        //2.循环1到n的全部数字
        for (int i = 1; i <= n; i++) {
            if (i % 2 == 1) {   //3.判断是否为奇数
                sum += i;   //4.把是奇数的累加
            }
        }
        System.out.println("1到" + n + "的奇数和是" + sum);
        return sum;
    }

    public static int test2(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i += 2) {
            sum += i;
        }
        System.out.println("1到" + n + "的奇数和是" + sum);
        return sum;
    }
}
